package com.ziq.zcamp.place

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.GsonUtils
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.base.extension.dp
import com.ziq.zcamp.bean.place.CampListItem
import com.ziq.zcamp.bean.place.PlaceListItem
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.databinding.ItemPlaceBannerBinding
import com.ziq.zcamp.login.ActionAfterLogin
import com.ziq.zcamp.login.LoginActivity
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.place.PlaceListFragmentViewModel.Companion.empty_id
import com.ziq.zcamp.util.ImageUtil
import com.ziq.zcamp.view.SafetySimpleDraweeView
import java.text.DecimalFormat

class CampBannerAdapter : RecyclerView.Adapter<BindingViewHolder<ItemPlaceBannerBinding>>() {

        init {
            setHasStableIds(true)
        }

        var onPlaceClickListener : OnPlaceClickListener? = null
        private var dataList : List<CampListItem>? = null
        fun setData(list: List<CampListItem>?) {
            dataList = list
            notifyDataSetChanged()
        }

        fun getDataItem(position: Int) : CampListItem?{
            if(dataList != null){
                val length = dataList!!.size
                if(position in 0 until length){
                    return dataList!![position]
                }
            }
            return null
        }

        fun getPosition(item : CampListItem):Int{
            if(dataList?.isNotEmpty() == true){
                return dataList?.indexOf(item)?:-1
            }
            return -1
        }

        override fun getItemCount(): Int {
            return dataList?.size?:0
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemPlaceBannerBinding> {
            return createBindingViewHolder(parent)
        }

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemPlaceBannerBinding>,
            position: Int
        ) {
            val item = dataList?.get(position)
            item?.let {
                var leftMargin = 0.dp
                var rightMargin = 0.dp
                if(position == 0){
                    leftMargin = 12.dp
                }
                if(position == itemCount - 1){
                    rightMargin = 12.dp
                }

                val lp = holder.binding.cardview.layoutParams as ConstraintLayout.LayoutParams
                lp.rightMargin = rightMargin
                lp.leftMargin = leftMargin
                holder.binding.cardview.layoutParams = lp

                if(it.id == empty_id){
                    holder.binding.clInfo.visibility = View.GONE
                    holder.binding.llTopLabel.visibility = View.GONE
                    holder.binding.llMoreStatus.visibility = View.VISIBLE

                    if(position == 0){
                        holder.binding.tvMoreStatus.text = "暂无相关营地"
                    } else {
                        holder.binding.tvMoreStatus.text = "筛选结果展示完毕"
                    }
                    holder.binding.cardview.click {}

                } else {
                    holder.binding.clInfo.visibility = View.VISIBLE
                    holder.binding.llMoreStatus.visibility = View.GONE

                    holder.binding.name.text = item.campName

                    holder.binding.cardview.click {
                        onPlaceClickListener?.onPlaceClick(item)
                    }

//                    if(item.tag != null && item.tag?.position == 1){
//                        holder.binding.ivTag.visibility = View.VISIBLE
//                        val lp = holder.binding.ivTag.layoutParams
//                        lp.width = item.tag?.image_width?:81.dp
//                        lp.height = item.tag?.image_height?:18.dp
//                        holder.binding.ivTag.layoutParams = lp
//                        ImageUtil.updateImageFromNetwork(holder.binding.ivTag, item.tag?.image?:"")
//                    } else {
//                        holder.binding.ivTag.visibility = View.GONE
//                    }
//
                    for ((index, imageView) in holder.binding.llImages.children.withIndex()){
                        if(imageView is SafetySimpleDraweeView){
                            var imageList: List<ImageInfo> =  mutableListOf<ImageInfo>()
                            if (item.pictures?.isNotEmpty() == true){
                                item.pictures?.let {
                                    imageList = GsonUtils.fromJson(it, GsonUtils.getListType(
                                        ImageInfo::class.java))
                                }
                            }
                            if(imageList?.isNotEmpty() == true){
                                if(index < (imageList?.size?:0)){
                                    val imageInfo = imageList?.get(index)
                                    imageView.visibility = View.VISIBLE
                                    if (imageInfo?.source?.isNotEmpty() == true){
                                        ImageUtil.updateImageFromNetwork(imageView, imageInfo.source)
                                    }else{
                                        ImageUtil.updateImageFromNetwork(imageView, imageInfo?.url)
                                    }
                                } else {
                                    imageView.visibility = View.INVISIBLE
                                    ImageUtil.updateImageFromNetwork(imageView, "")
                                }
                            } else {
                                imageView.visibility = View.INVISIBLE
                                ImageUtil.updateImageFromNetwork(imageView, "")
                            }
                        }
                    }
//
//                    var hasTopLabel = item.rank?.country_rank != null || item.rank?.province_rank != null
//                    if(hasTopLabel){
//                        holder.binding.llTopLabel.visibility = View.VISIBLE
//                        if(item.rank?.country_rank != null){
//                            holder.binding.tvTopLabel.text = "全国热门营地榜·Top${item.rank?.country_rank?.rank}"
//                        } else if(item.rank?.province_rank != null){
//                            holder.binding.tvTopLabel.text = "${item.rank?.province_rank?.district?.name}热门营地榜·Top${item.rank?.province_rank?.rank}"
//                        }
//                    } else {
//                        holder.binding.llTopLabel.visibility = View.GONE
//                        holder.binding.tvTopLabel.text = ""
//                    }


                    val distance = item.distance?.toFloat()
                    if(distance != null){
                        distance.let {
                            holder.binding.tvLabelDistance.text = "${DecimalFormat("0.##").format(distance / 1000)}km"
                        }
                    } else {
                        holder.binding.tvLabelDistance.text = ""
                    }


                    var hasScore = false
                    if (item.score == null || item.score == 0.0){
                        hasScore = false
                        holder.binding.llLabelStars.visibility = View.GONE
                    }else{
                        hasScore = true
                        holder.binding.llLabelStars.visibility = View.VISIBLE
                        holder.binding.tvLabelStars.text = "${DecimalFormat("0.0").format((item.score?:0.0) / 10f)}分"
                    }

//                    var hasCouponLabel = item.coupon_tag?.stock_id?.isNotEmpty() == true
//                    if(hasCouponLabel){
//                        holder.binding.llLabelCoupon.visibility = View.VISIBLE
//                        holder.binding.tvLabelCoupon.text = "${item.coupon_tag?.coupon_title}"
//                    } else {
//                        holder.binding.llLabelCoupon.visibility = View.GONE
//                        holder.binding.tvLabelCoupon.text = ""
//                    }
//
//                    if(hasCouponLabel){
//                        holder.binding.llLabelVr.isVisible = false
//                        holder.binding.llLabelBook.isVisible = false
//                    } else {
//                        holder.binding.llLabelVr.isVisible = item.exist_vr == true
//                        holder.binding.llLabelBook.isVisible = item.exist_selling_commodity == true
//                    }
//
//
//                    holder.binding.btnCollect.isSelected = item.is_collect?:false
//                    UserManager.instance.campCollect.value?.let {
//                        if(it.containsKey(item.id)){
//                            holder.binding.btnCollect.isSelected = it[item.id] == 0
//                        }
//                    }
//
//                    holder.binding.btnCollect.click {
//                        LoginActivity.checkLogin ( object : ActionAfterLogin {
//                            override fun action() {
//                                UserManager.instance.apply {
//                                    if(isLogin.value == true){
//                                        if(holder.binding.btnCollect.isSelected){
//                                            collectCamp(item.id?:0, 1, onSuccessAction = {
//                                                item.is_collect = false
//                                                holder.binding.btnCollect.isSelected = false
//                                            })
//                                        } else {
//                                            collectCamp(item.id?:0, 0, onSuccessAction = {
//                                                item.is_collect = true
//                                                holder.binding.btnCollect.isSelected = true
//                                            })
//                                        }
//                                    }
//                                }
//
//                            }
//                        })
//                    }



                }


            }

        }

    }